from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session, declarative_base

from app.config.config import config
from app.settings import env

def db_connect():
    # 创建一个引擎,目的是连接到本地的数据库
    config_class = config[env]()
    engine = create_engine(config_class.db_url,echo=config_class.if_echo,pool_size=10,max_overflow=30)
    # 打开数据库连接对话
    session = sessionmaker(engine)
    # 保证线程安全
    db_session = scoped_session(session)
    # 获取基类
    Base = declarative_base()
    return db_session, Base, engine